package com.zhengdongzulin.zuke.app.main.findhouse

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import com.baidu.mapapi.search.core.PoiInfo
import com.baidu.mapapi.search.poi.*
import com.lxj.androidktx.core.*
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.base.BaseViewModel
import com.zhengdongzulin.zuke.databinding.ZkActivityFindHouseSearchPoiBinding

class ZKFindHouseSearchPoiActivity :
    BaseActivity<BaseViewModel, ZkActivityFindHouseSearchPoiBinding>() {
    lateinit var mPoiSearch: PoiSearch
    var mData: MutableList<PoiInfo> = mutableListOf()

    override fun getLayoutResId(): Int = R.layout.zk_activity_find_house_search_poi

    override fun initVM(): BaseViewModel = BaseViewModel()

    override fun initView() {
        mView.contentView.setEnableRefresh(false)
        mView.search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                mPoiSearch.searchInCity(PoiCitySearchOption().apply {
                    city(mShareViewModel.city.value!!.cityName)
                    pageNum(0)
                    pageCapacity(10)
                    keyword(mView.search.text.toString())
                    cityLimit(true)
                    scope(2)//值为1 或 空，返回基本信息//值为2，返回POI详细信息
                })
            }
        })
    }

    override fun initData() {
        mPoiSearch = PoiSearch.newInstance()
        mPoiSearch.setOnGetPoiSearchResultListener(object : OnGetPoiSearchResultListener {
            override fun onGetPoiResult(p0: PoiResult?) {
                mData.clear()
                p0?.allPoi?.let { mData.addAll(it) }
                mView.recyclerView.updateData(mData)

                if (!mView.search.text.isEmpty() && mData.size == 0) {
                    mView.statusView.showEmpty()
                } else {
                    mView.statusView.showContent()
                }
            }

            override fun onGetPoiDetailResult(p0: PoiDetailResult?) {

            }

            override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {
                toast(p0.toString())
            }

            override fun onGetPoiIndoorResult(p0: PoiIndoorResult?) {
                toast(p0.toString())
            }

        })
        mView.recyclerView.vertical().bindData(
            mData,
            R.layout.item_poi_search_house
        ) { holder, item, position ->
            holder.setText(R.id.title_name, item.name)
            holder.setText(
                R.id.desc,
                "${item.poiDetailInfo.tag}${if (item.address.isNullOrEmpty()) "" else "\u0020-\u0020${item.address}"}"
            )
        }.itemClick { data: List<PoiInfo>, holder, position ->
            setResult(RESULT_OK, Intent().apply {
                putExtra("lat", data[position].location.latitude)
                putExtra("lng", data[position].location.longitude)
            })
            finish()
        }
    }

    override fun initListener() {

    }

    override fun startObserve() {

    }

    override fun onDestroy() {
        mPoiSearch.destroy()
        super.onDestroy()
    }
}